package com.polaris.live.ui.block

import android.os.Bundle
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.polaris.live.R
import com.polaris.live.adapter.BlockAdapter
import com.polaris.live.common.mvvm.base2.BaseVbActivity
import com.polaris.live.common.network.parseState
import com.polaris.live.common.util.ToastGravity
import com.polaris.live.databinding.ActivityBlockBinding
import com.polaris.live.translation.R.string
import com.polaris.live.viewmodel.BlackViewModel

/**
 * BlockActivity
 *
 * @author Created by 半仙 on 2023/9/4/004 14:32
 */
class BlockActivity : BaseVbActivity<BlackViewModel, ActivityBlockBinding>() {

    private val mAdapter: BlockAdapter by lazy {
        BlockAdapter()
    }

    override fun initView(savedInstanceState: Bundle?) {
        mBinding.recyclerView.setEnableRefresh(false)
        mBinding.recyclerView.setEmptyTip(R.drawable.ic_block_empty, getString(string.no_block))
        mBinding.recyclerView.apply {
            recycler.layoutManager = GridLayoutManager(context, 3, RecyclerView.VERTICAL, false)
            recycler.itemAnimator = null
            attachedAdapter(mAdapter, mViewModel).observe(
                this@BlockActivity,
                mViewModel.blackLiveData
            )
        }

        mViewModel.blackLiveData.observe(this) { resultState ->
            parseState(resultState, {

            },{
                ToastGravity.showGravity(it.errorMsg)
            })
        }

        mViewModel.positionLiveData.observe(this) {
            mAdapter.removeAt(it)
        }

        mViewModel.onRefresh()

        mAdapter.setOnItemChildClickListener { _, view, position ->
            when (view.id) {
                R.id.block_ll -> {
                    mViewModel.cancelBlack(mAdapter.data[position].userId, position)
                }
                R.id.block_item_head ->{
                    ToastGravity.showGravity(getString(string.no_access))
                }
            }
        }
    }
}